package com.jp.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

public class LambdaUsage {

    private static List<Apple>  filter(List<Apple> list, Predicate<Apple> predicate){
        List<Apple> resultList = new ArrayList<>();
        for(Apple apple : list){
            if(predicate.test(apple)){
                resultList.add(apple);
            }
        }
        return resultList;
    }

    private static List<Apple>  filterByWeight(List<Apple> list, LongPredicate longPredicate){
        List<Apple> resultList = new ArrayList<>();
        for(Apple apple : list){
            if(longPredicate.test(apple.getWeight())){
                resultList.add(apple);
            }
        }
        return resultList;
    }
    private static List<Apple>  filterByBiPredicate(List<Apple> list, BiPredicate<String,Long> biPredicate){
        List<Apple> resultList = new ArrayList<>();
        for(Apple apple : list){
            if(biPredicate.test(apple.getColor(),apple.getWeight())){
                resultList.add(apple);
            }
        }
        return resultList;
    }



    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150),
                new Apple("yellow",140),new Apple("red",170));

        List<Apple> result;
        result=filter(list,(apple)->"green".equals(apple.getColor()));
        System.out.println(result);

        result=filterByWeight(list,l->l==140);
        System.out.println(result);

        result=filterByBiPredicate(list,(color,weight)->{
            if("green".equals(color) && weight>100)
                return true;
            return false;
        });
        System.out.println(result);


        int i=0;
//        Runnable r  = new Runnable(){
//            public void run(){
//                System.out.println(i); //此处会报错，因为i不是final
//            }
//        };
//        r.run();

        Runnable r1  = ()->System.out.println(i);
        r1.run();
    }
}
